package com.ling.module1sbbyyds.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * @Description: 类描述
 * @author: ling
 * @date: 2023年03月04日
 */
@Component
public class RedisDataUtil {

    @Autowired
    private RedisTemplate<String, String> redis;

    /**
     * @param code: 验证码
     * @param phone 手机号
     * @Description: 验证码验证  (只用一次，用后删除)
     * @author: ling
     * @date: 2022/3/27
     * @Return: boolean
     */
    public boolean verifyCode(String code, String phone) {
        if (ObjectUtils.isEmpty(code) || ObjectUtils.isEmpty(phone)) {
            return false;
        }
        //redis中的验证码
        String codeValue = redis.opsForValue().get(phone);
        if (codeValue != null && codeValue.equals(code)) {
            redis.delete(phone);
            return true;
        }
        return false;
    }
}
